package de.ready4fajir.anywear;

import de.ready4fajir.anywear.persistence.*;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;

/**
 * Entry point for the application.
 * @author FMannan
 *
 */
public class ProfileList extends ListActivity {
	
	/**
	 * Here is where our Profiles are being saved.
	 */
	private ProfileOpenHelper profiles;
	private static int [] TO = {R.id.profileName};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        profiles = new ProfileOpenHelper(this);
        
        try
        {
        	profiles.dropAll();
        	profiles.addProfile("Home", false);
        	profiles.addProfile("Office", true);
       	
        	Cursor cursor = profiles.getProfiles();
        	//TODO what is the cursor is null?
        	startManagingCursor(cursor);        	
        	//Show profiles in a list view        	
        	showProfiles(cursor);
        } 
        finally
        {
        	profiles.close();
        }        
    }
    
    
    /**
     * Displays the available profiles to the user.
     */
    private void showProfiles(Cursor cursor)
    {
    	SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.profileentry, cursor, ProfileOpenHelper.FROM, TO);
    	setListAdapter(adapter);   	
    }
}